$(function () {
    $("#jqGrid").Grid({
        url: '../userasset/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '用户id', name: 'uid', index: 'uid', width: 80,align:'center'},
            {label: '手机号', name: 'phone', index: 'phone', width: 80,align:'center'},
            {label: '用户名', name: 'userName', index: 'user_name', width: 80,align:'center'},
            {label: '总资金(返佣)', name: 'amount', index: 'amount', width: 40,align:'center'},
            {label: '冻结', name: 'frozen', index: 'frozen', width: 40,align:'center'},
            {label: '链总数量', name: 'coin', index: 'coin', width: 40,align:'center'},
            {label: '链冻结', name: 'coinFrozen', index: 'coinFrozen', width: 40,align:'center'},
            {label: '普通', name: 'kilometer', index: 'kilometer', width: 40,align:'center'},
            {label: '额外', name: 'kilometerExtra', index: 'kilometer_extra', width: 50,align:'center'},
            {label: '运动距离', name: 'kilometerReal', index: 'kilometer_real', width: 50,align:'center'},
            {label: 'SPC数量', name: 'spcCoin', index: 'spcCoin', width: 50,align:'center'},
            {label: 'SPC收益冻结', name: 'spcCoinFrozen', index: 'spcCoinFrozen', width: 50,align:'center'},
            {label: '每日返还利率', name: 'backRate', index: 'backRate', width: 50,align:'center'},
            {
                label: '是否是Done', name: 'done', index: 'done', width: 30, formatter: function (value) {
                    if (value == 1) {
                        return "是";
                    } else if (value == 0) {
                        return "否";
                    }
                    return "其他";
                }
            },
            {label: '创建时间', name: 'createDate', index: 'create_date', width: 80}]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        tUserAsset: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            phone: '',
            done: '',
            sort: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.tUserAsset = {};
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.tUserAsset.id == null ? "../userasset/save" : "../userasset/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.tUserAsset),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {

                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../userasset/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../userasset/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.tUserAsset = r.tUserAsset;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name, 'phone': vm.q.phone, 'done': vm.q.done, 'sort': vm.q.sort},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                phone: '',
                done: '',
                sort: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }, exportPro: function () {
            var strhref = "../userasset/export?name=" + vm.q.name;
            if (vm.q.phone != '')
                strhref += "&phone=" + vm.q.phone;
            if (vm.q.done != '')
                strhref += "&done=" + vm.q.done;
            if (vm.q.sort != '')
                strhref += "&sort=" + vm.q.sort;
            let index = $("#jqGrid").jqGrid('getGridParam', 'page');
            strhref += "&index=" + index;
            strhref += "&size=" + 50;
            location.href = strhref;
        }
    }
});